BEGIN;
CREATE TABLE `basic_author` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `name` varchar(45) NOT NULL,
    `surname` varchar(45) NOT NULL,
    `birth` date
)
;
CREATE TABLE `basic_publisher` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `name` varchar(45) NOT NULL UNIQUE
)
;
CREATE TABLE `basic_media` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `name` varchar(2) NOT NULL UNIQUE
)
;
CREATE TABLE `basic_libraryuser` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `user_id` integer NOT NULL UNIQUE
)
;
ALTER TABLE `basic_libraryuser` ADD CONSTRAINT `user_id_refs_id_2b9bcc2` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`);
CREATE TABLE `basic_librarystaff` (
    `libraryuser_ptr_id` integer NOT NULL PRIMARY KEY
)
;
ALTER TABLE `basic_librarystaff` ADD CONSTRAINT `libraryuser_ptr_id_refs_id_fa83323` FOREIGN KEY (`libraryuser_ptr_id`) REFERENCES `basic_libraryuser` (`id`);
CREATE TABLE `basic_item` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `title` varchar(45) NOT NULL,
    `date` date NOT NULL,
    `author_id` integer NOT NULL,
    `media_id` integer NOT NULL,
    `publisher_id` integer NOT NULL,
    `isbn` numeric(13, 0) UNIQUE,
    `url` varchar(200),
    `is_available` bool NOT NULL,
    `is_digital` bool NOT NULL
)
;
ALTER TABLE `basic_item` ADD CONSTRAINT `publisher_id_refs_id_8a8ef755` FOREIGN KEY (`publisher_id`) REFERENCES `basic_publisher` (`id`);
ALTER TABLE `basic_item` ADD CONSTRAINT `media_id_refs_id_949f2a69` FOREIGN KEY (`media_id`) REFERENCES `basic_media` (`id`);
ALTER TABLE `basic_item` ADD CONSTRAINT `author_id_refs_id_319ee3bb` FOREIGN KEY (`author_id`) REFERENCES `basic_author` (`id`);
CREATE TABLE `basic_history` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `library_user_id` integer NOT NULL,
    `item_id` integer NOT NULL,
    `due_date` date NOT NULL
)
;
ALTER TABLE `basic_history` ADD CONSTRAINT `item_id_refs_id_6952687d` FOREIGN KEY (`item_id`) REFERENCES `basic_item` (`id`);
ALTER TABLE `basic_history` ADD CONSTRAINT `library_user_id_refs_id_a530ae59` FOREIGN KEY (`library_user_id`) REFERENCES `basic_libraryuser` (`id`);
CREATE TABLE `basic_order` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `library_user_id` integer NOT NULL,
    `item_id` integer NOT NULL,
    `due_date` date NOT NULL,
    `is_ready` bool NOT NULL
)
;
ALTER TABLE `basic_order` ADD CONSTRAINT `item_id_refs_id_ad85ea23` FOREIGN KEY (`item_id`) REFERENCES `basic_item` (`id`);
ALTER TABLE `basic_order` ADD CONSTRAINT `library_user_id_refs_id_8935ebbf` FOREIGN KEY (`library_user_id`) REFERENCES `basic_libraryuser` (`id`);
CREATE INDEX `basic_item_cc846901` ON `basic_item` (`author_id`);
CREATE INDEX `basic_item_11f50c51` ON `basic_item` (`media_id`);
CREATE INDEX `basic_item_22dd9c39` ON `basic_item` (`publisher_id`);
CREATE INDEX `basic_history_71c77b8d` ON `basic_history` (`library_user_id`);
CREATE INDEX `basic_history_67b70d25` ON `basic_history` (`item_id`);
CREATE INDEX `basic_order_71c77b8d` ON `basic_order` (`library_user_id`);
CREATE INDEX `basic_order_67b70d25` ON `basic_order` (`item_id`);
COMMIT;
